package ihuotui.test.getmapdata;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;

public class GetInfoClient {
    
    // The user agent
    protected final String USER_AGENT = "Mozilla/5.0";

    // The host 
    protected static final String host = "http://www.serta.cn";

    private CloseableHttpClient httpclient;
    
    public GetInfoClient(int maxConnectPool) {

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(maxConnectPool);

        httpclient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();
    }
    
    public SampleResponse send(String msg) throws Exception {
        HttpPost post = new HttpPost("http://www.serta.cn/tools/map.ashx");
        
        post.setHeader("User-Agent", USER_AGENT);
        post.setHeader("Accept", "*/*");
        post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//        
        List<NameValuePair> ls = new ArrayList<NameValuePair>();	
		ls.add(new BasicNameValuePair("action", "GetStoresDataOnMap"));
		ls.add(new BasicNameValuePair("PromaryId", "0"));
		ls.add(new BasicNameValuePair("cityName", msg));
		ls.add(new BasicNameValuePair("brandName", null));
		ls.add(new BasicNameValuePair("mix", null));
//		
//			HttpEntity entity = 
//			Request.Post("http://61.1xx.xxx.29:xxxx/Mxxx/wxxx.axxx/MxxxxxSubmit")
//				.connectTimeout(3000)			
//				.body(new UrlEncodedFormEntity(ls,"UTF-8"))
//				.execute().returnResponse().getEntity();
//			
//			entity.writeTo(System.out);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

		//        String postBody = null;
//        StringEntity se = new StringEntity(postBody, "UTF-8");
        HttpEntity se=new UrlEncodedFormEntity(ls,"UTF-8");
        post.setEntity(se);
        
        
        // Send the post request and get the response
        HttpResponse response = httpclient.execute(post);
        SampleResponse res=new SampleResponse();
        int status = response.getStatusLine().getStatusCode();
        res.setResponseCode(status);
        BufferedReader rd =
                new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        res.setResponseJson(result.toString());
        if (status == 200) {
            res.setResult(true);
        } else {
            res.setResult(false);
        }
        return res;
    }
    
}
